package com.water.wfo.component.api.domain;

import com.water.wfo.component.api.filter.RequestFilter;
import com.water.wfo.component.api.interceptor.AuthenticationInterceptor;
import com.water.wfo.component.api.utils.TokenMarshal;
import com.water.wfo.component.util.StringUtil;
import com.water.wfo.component.util.UUIDUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * packageName com.water.wfo.sys.pojo.context
 *
 * @author
 * @className WfoCookie(处以class为网)
 * @date 2025/1/15
 * @description T0D0
 */
public final class WfoCookie {
    private static WfoCookie cookie;

    private WfoCookie(){}

    public static WfoCookie getInstance(){
        if(cookie == null){
            cookie = new WfoCookie();
        }
        return cookie;
    }

    public static String getTokenParam(){
        Cookie cookie = AuthenticationInterceptor.getCookie("_TOKEN");
        if(cookie == null){
            cookie = new Cookie("_TOKEN", StringUtil.createToken());
        }
        String value = cookie.getValue();
        value = StringUtils.replace(value, "-", "+");
        value = StringUtils.replace(value, "_", "/");

        try {
            String encodeToken = new String(Base64.decodeBase64(value),"UTF-8");
            String[] keys = encodeToken.split(",");
            String userId = keys[1];
            String deptCode = keys[3];
            CookieToken token =new CookieToken(UUIDUtils.getUuid(),userId,userId,deptCode,36000);
            if(keys.length > 5){
                token.setDeptName(keys[4]);
            }
            return TokenMarshal.marshal(token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void saveCookie(){
       HttpServletResponse response = (HttpServletResponse) RequestFilter.getResponseMap().get();
       String tokenParam = getTokenParam();
       Cookie cookied =getCookie();
       if(cookied != null){
           cookied.setValue(tokenParam);
       }else {
           cookied = new Cookie("_TOKEN",tokenParam);
       }

       cookied.setPath("/");
       cookied.setDomain(".water.com");
       response.addCookie(cookied);
    }

    public static void invalidateCookie(){
        HttpServletResponse response = (HttpServletResponse) RequestFilter.getResponseMap().get();
        Cookie cookie_token = getCookie();
        if(cookie_token != null){
            cookie_token.setMaxAge(0);
            cookie_token.setDomain(".water.com");
            response.addCookie(cookie_token);
        }
    }

    public static Cookie getCookie(){
        return getCookie("_TOKEN");
    }
    public static Cookie getCookie(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        } else {
            HttpServletRequest request = (HttpServletRequest)RequestFilter.getRequestMap().get();
            Cookie[] cookies = request.getCookies();
            if (cookies != null && cookies.length > 0) {
                for(int i = 0; i < cookies.length; ++i) {
                    if (name.equals(cookies[i].getName())) {
                        return cookies[i];
                    }
                }
            }

            return null;
        }
    }
}
